package com.install.manager;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.set.ListOrderedSet;

import com.dcits.common.util.StringEx;
import com.install.core.manager.CommManager;
import com.install.core.util.SimpleTreeNode;
import com.install.core.util.TreeNodeBulid;
import com.install.core.util.UserUtil;

public class IndexManager extends CommManager {
	public IndexManager() {
		super(true);
	}

	public JSONObject getGns(JSONArray object, UserUtil userUtil) {
		JSONObject json = new JSONObject();
		String sql = "select gns.gns_dm,(select case when count(1)>0 then 'false' else 'true' end as isleaf from dm_gns z where z.sjgn_dm=gns.gns_dm)  " +
				"isleaf,IFNULL(gns.sjgn_dm,'0') sjgn_dm,gns.gns_mc FROM user u ,dm_rylx_qx rq,dm_qx_gns qg,dm_gns gns " +
				"where u.rylx=rq.lx_dm and rq.qx_dm=qg.qx_dm and qg.gns_dm=gns.gns_dm " +
				"and  FIND_IN_SET(gns.gns_dm, getChildLst(gns.gns_dm))and U.uid='"+userUtil.getUid()+"'";
		JSONArray result = this.listWithparams(sql,null);
		List treeList=new ArrayList();
		if (result.size() > 0) {
			json.element("gns", result);
			for(int i=0;i<result.size();i++){
				JSONObject node = result.getJSONObject(i);
				String id1=StringEx.sNull(node.getString("GNS_DM"));
				String pid1=StringEx.sNull(node.getString("SJGN_DM"));
				boolean isleaf=StringEx.sNull(node.getString("ISLEAF")).equals("true")?true:false;
				String text=StringEx.sNull(node.getString("GNS_MC"));
				SimpleTreeNode tree=new SimpleTreeNode(id1,pid1,isleaf,text);
				treeList.add(tree);
			}
		}
		treeList=TreeNodeBulid.buildTree("0", treeList);
		json.element("treeList", treeList); 
		System.out.println(treeList);
		return json;
	}
}
